package com.cqc.lite.web;

import com.cqc.lite.domain.Role;
import com.cqc.lite.domain.SysLog;
import com.cqc.lite.domain.UserInfo;
import com.cqc.lite.service.RoleService;
import com.cqc.lite.service.SysLogService;
import com.cqc.lite.service.UserService;
import com.cqc.lite.utils.CommonIOUtil;
import com.cqc.lite.utils.CsvUtil;
import com.cqc.lite.utils.DateUtils;
import com.cqc.lite.utils.JavaCSVUtil;
import com.cqc.lite.vo.DataGridVO;
import com.cqc.lite.vo.UserQryVO;
import com.csvreader.CsvWriter;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.StringBuilderWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.util.*;

@Controller
@RequestMapping("/sysLog/")
public class SysLogController {

    private final SysLogService sysLogService;

    @Autowired
    public SysLogController(SysLogService sysLogService){
        this.sysLogService=sysLogService;
    }


    @GetMapping("findAll.do")
    public ModelAndView findAll(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("syslog-list");
        //查询所有的日志
        List<SysLog> sysLogs=sysLogService.findAll();
        mv.addObject("sysLogs",sysLogs);
        return mv;
    }


    @RequestMapping("findAllByPage.do")
    @ResponseBody
    public Map<String,Object> findAllByPage(DataGridVO dataGridVO, UserQryVO qryVO){
        int pageSize=dataGridVO.getRows();
        int offSet=(dataGridVO.getPage()-1)*pageSize;
        int total = sysLogService.findCount();
        List<SysLog> sysLogs=sysLogService.findAllByPage(offSet,pageSize);

        Map<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rows",sysLogs);
        return map;
    }

    @RequestMapping("findAllByPage2.do")
    @ResponseBody
    public Map<String,Object> findAllByPage2(DataGridVO dataGridVO, UserQryVO qryVO){
        int pageSize=dataGridVO.getRows();
        int offSet=(dataGridVO.getPage()-1)*pageSize;
        int total = sysLogService.findCount();
        List<SysLog> sysLogs=sysLogService.findAllByPage(offSet,pageSize);

        Map<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rows",sysLogs);
        return map;
    }

    @PostMapping("uploadFile.do")
    public void uploadFile(@RequestParam MultipartFile multipartFile, String username){
        List<List<String>> lists = CsvUtil.readCsv(multipartFile);
        for (List<String> list : lists) {
            System.out.println(list);
        }
    }

    @GetMapping("exportFile.do")
    public void exportFile(HttpServletRequest request, HttpServletResponse response){
        String filename = DateUtils.date2String(new Date(),DateUtils.PATTERN_YYYY_MM_DD_HH_MM_SS) + "exportFile.csv";
        List<Object> headList = new ArrayList<Object>();
        headList.add("第一列");
        headList.add("第二列");
        headList.add("第三列");
        List<List<Object>> dataList = new ArrayList<>();
        List<Object> data = new ArrayList<>();
        data.add("111");
        data.add("222");
        data.add("333");
        dataList.add(data);

        CsvUtil.downloadCsv(request,response,headList,dataList,filename);
    }
// java-csv.jar
    @PostMapping("uploadFileByJavaCSV.do")
    public void uploadFileByJavaCSV(@RequestParam MultipartFile multipartFile, String username){
        List<String[]> lists = JavaCSVUtil.readCsv(multipartFile);
        for (String[] arr : lists) {
            System.out.println(Arrays.toString(arr));
        }
    }
    @GetMapping("exportFileByJavaCSV.do")
    public void exportFileByJavaCSV(HttpServletRequest request, HttpServletResponse response){
        String filename = DateUtils.date2String(new Date(),DateUtils.PATTERN_YYYY_MM_DD_HH_MM_SS) + "exportFileByJavaCSV.csv";
        String[] head = new String[]{"第一列","第二列","第三列"};
        List<String[]> dataList = new ArrayList< String[]>();
        dataList.add(new String[]{"1","2","3"});
        dataList.add(new String[]{"4","5","6"});

        JavaCSVUtil.downloadCsv(request,response,head,dataList,filename);
    }

//common-io.jar
    @GetMapping("exportFileByCommonIO.do")
    public void exportFileByCommonIO(HttpServletRequest request, HttpServletResponse response){
        //IOUtils.write("姓名,年龄,地址",os,StandardCharsets.UTF_8);//写入一行数据,注意不会自动换行
        List<String> lines = new ArrayList<>();
        lines.add("姓名,年龄,地址");
        lines.add("张三,18,上海市");
        lines.add("李四,20,北京市");
        CommonIOUtil.downloadCsv(request,response,lines,"common-io.csv");
    }

    @PostMapping("uploadFileByCommonIO.do")
    public void uploadFileByCommonIO(@RequestParam MultipartFile multipartFile, String username){
        List<String[]> lists = CommonIOUtil.readCsv(multipartFile);
        for (String[] arr : lists) {
            System.out.println(Arrays.toString(arr));
        }
    }


}
